import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:flutter_multi_select_items/flutter_multi_select_items.dart';
import 'package:get/get.dart';

class SpecializedPosition {

  static void show(title, context, {
    Function? onConfirm,
    required List<Position> selectedPosition
  }) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus!.unfocus();
    }

    String? selectedCode = selectedPosition.map((e) => e.code).toString();
    List<Position> positions = _getPositionData(selectedCode);

    showModalBottomSheet(
      context: Get.context!,
      backgroundColor: Colors.transparent,
      builder: (context) {
        return Material(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10.r),
            topRight: Radius.circular(10.r)
          ),
          child: Container(
            padding: const EdgeInsets.only(top: 4),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      TextButton(
                        style: TextButton.styleFrom(
                          minimumSize: Size(ButtonTheme.of(context).minWidth, 42),
                        ),
                        onPressed: () {
                          Get.back();
                        }, 
                        child: Text('取消', style: TextStyle(fontSize: 15.sp, color: Colors.grey),)
                      ),

                      TextButton(
                        style: TextButton.styleFrom(
                          minimumSize: Size(ButtonTheme.of(context).minWidth, 42),
                        ),
                        onPressed: () {
                          onConfirm?.call(selectedPosition);
                        }, 
                        child: Text('确认', style: TextStyle(fontSize: 15.sp, color: const Color(0xff499360)),)
                      )
                    ],
                  ),
                ),

                _buildPositionItem(positions, selectedCode, onChange: (allSelectedItems, selectedItem) {
                  Position position = selectedItem as Position;
                  if (selectedCode?.contains(position.code) == true) {
                    selectedPosition.removeWhere((element) => element.code == position.code);
                    selectedCode?.replaceAll(position.code, '');
                  } else if (selectedPosition.length < 3) {
                    selectedPosition.add(position);
                    selectedCode = '$selectedCode,${position.code}';
                  } else {
                    CustomToast.showTextToast('最多只能选择3个擅长位置');
                  }
                }),

                SizedBox(height: 50.w,)
              ],
            ),
          )
        );
      }
    );
  }


  static _buildPositionItem(List<Position>? position, String? selectedCode, {
    Function? onChange
  }) {
    if (position == null) {
      return Container();
    }
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 5.w),
      child: MultiSelectContainer(
        maxSelectableCount: 3,
        // itemsPadding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 15.w),
        items: position.map((element) {
          return MultiSelectCard(
            value: element,
            selected: element.isSelected,
            label: element.name
          );
        }).toList(),
        wrapSettings: WrapSettings(
          runSpacing: 10.w
        ),
        textStyles: MultiSelectTextStyles(
          textStyle: TextStyle(color: const Color(0xff333333), fontSize: 14.sp),
          selectedTextStyle: TextStyle(color: Colors.white, fontSize: 14.sp),
        ),
        itemsDecoration: MultiSelectDecorations(
          selectedDecoration: BoxDecoration(
            color: const Color(0xff499360),
            borderRadius: BorderRadius.circular(22.r),
          ),
          decoration: BoxDecoration(
            color: const Color(0xff999999).withOpacity(0.15),
            borderRadius: BorderRadius.circular(22.r),
          ),
        ),
        onChange: (allSelectedItems, selectedItem) {
          onChange?.call(allSelectedItems, selectedItem);
        },
        onMaximumSelected: (selectedItems, selectedItem) {
          CustomToast.showTextToast('最多只能选择3个擅长位置');
        },
      ),
    );
  }

  static _getPositionData(String? selected) {
    List<Position> positions = [];

    ///添加后卫
    positions.add(Position('中后卫', 'CB', selected?.contains('CB') == true));
    positions.add(Position('清道夫', 'SW', selected?.contains('SW') == true));
    positions.add(Position('左边后卫', 'LB', selected?.contains('LB') == true));
    positions.add(Position('右边后卫', 'RB', selected?.contains('RB') == true));

    ///添加自由人
    positions.add(Position('自由人', 'LIB', selected?.contains('LIB') == true));

    ///添加守门员
    positions.add(Position('守门员', 'GK', selected?.contains('GK') == true));

    ///添加中场
    positions.add(Position('防守型中场/后腰', 'DMF', selected?.contains('DMF') == true));
    positions.add(Position('中前卫', 'CMF', selected?.contains('CMF') == true));
    positions.add(Position('左中前卫', 'LMF', selected?.contains('LMF') == true));
    positions.add(Position('右中前卫', 'RMF', selected?.contains('RMF') == true));
    positions.add(Position('前腰', 'AMF', selected?.contains('AMF') == true));

    ///添加前锋
    positions.add(Position('影子前锋', 'SS', selected?.contains('SS') == true));
    positions.add(Position('中锋', 'CF', selected?.contains('CF') == true));
    positions.add(Position('左边前锋', 'LWF', selected?.contains('LWF') == true));
    positions.add(Position('右边前锋', 'RWF', selected?.contains('RWF') == true));

    return positions;
  }


}

class Position {
  String code;
  String name;
  bool isSelected = false;

  Position(this.name, this.code, this.isSelected);
}
